<template>
  <div>
    <el-table
      :data="specsList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="规格编号"> </el-table-column>
      <el-table-column prop="specsname" label="规格名称"> </el-table-column>
      <el-table-column label="规格属性">
        <template slot-scope="scoped">
          <div>
            <el-tag v-for="(item, index) in scoped.row.attrs" :key="index">{{
              item
            }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scoped">
          <div>
            <el-button type="success" v-if="scoped.row.status">启 用</el-button>
            <el-button type="info" v-else>禁 用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scoped">
          <div>
            <el-button type="primary" @click="edit(scoped.row.id)"
              >编 辑</el-button
            >
            <el-button @click="del(scoped.row.id)">删 除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 
          分页
        background      背景颜色
        layout          分页布局
        total           总条目数
        page-size       每页展示的条数
     -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="specsCount"
      :page-size="specsSize"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
</template>

<script>
import { successAlert, warningAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
import { specsDelete } from "../../../request/api";
export default {
  data() {
    return {};
  },
  methods: {
    ...mapActions({
      asyncSpecsList: "specs/asyncChangeList",
      asyncSpecsCount: "specs/asynChangeCount",
      asyncSpecsPage: "specs/asyncChangePgae",
    }),
    //改变当前页
    changePage(e) {
      this.asyncSpecsPage(e);
    },
    del(id) {
      specsDelete({ id: id }).then((res) => {
        if (res.data.msg == 200) {
          successAlert(res.data.msg);
          //重新请求列表数据
          this.asyncSpecsList();
          //   重新请求总条数
          this.asyncSpecsCount();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    edit(id) {
      this.$emit("edit", id);
    },
  },
  computed: {
    ...mapGetters({
      specsList: "specs/list",
      specsCount: "specs/count",
      specsSize: "specs/size",
    }),
  },
  mounted() {
    this.asyncSpecsCount(), this.asyncSpecsPage(1);
  },
};
</script>

<style>
</style>